package com.thread.thread5;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author : leimingchao
 * @date Date : 2021年11月30日 20:38
 */
public class CompletableFutureTest3 {


    public static void main(String[] args) {

        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        System.out.println("主线程开始。。。。。。。。");
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品基本数据。。。。");
            return "小米";
        }).whenComplete((r,e) -> {
            System.out.println("结果是：" + r);
        });

        CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品属性数据。。。。");
            return 1;
        }).whenComplete((r,e) -> {
            System.out.println("结果是：" + r);
        });

        CompletableFuture<String> completableFuture3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品营销数据。。。。");
            return "满199减100";
        }).whenComplete((r,e) -> {
            System.out.println("结果是：" + r);
        });

        //所有人都执行完
        CompletableFuture<Void> allOf = CompletableFuture.allOf(completableFuture, completableFuture2, completableFuture3);
        // Void aVoid = allOf.get();
        //线程插队
        allOf.join();
        System.out.println("所有人都完成了。。。。。。");


        //有一个执行完就行
        //CompletableFuture.anyOf(completableFuture,completableFuture2,completableFuture3);

        //以后异步任务的编程模式。。。
        // CompletableFuture.supplyAsync(() -> {},threadPool).whenComplete();

        threadPool.shutdown();
    }
}
